package com.jdcjk.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.jdcjk.Constant;
import com.jdcjk.MainApp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class PreferencesUtils {
    // sharepreference数据存放文件名
    public static final String LOGIN_USER = "login_user";
    private static SharedPreferences sharedPreferences = null;
    //信息推送设置 键值
    public static final String PUSH_INFO_SWITCH = "push_info_switch";
    //声音设置 键值
    public static final String VOICE_SWITCH = "voice_switch";
    //震动设置 键值
    public static final String VIBRATION_SWITCH = "vibration_switch";
    //报警信息数量
    public static final String WARNING_INFO_NUMBER = "warning_info_number";
    //地图类型 键值
    public static final String MAP_TYPE = "map_type";
    //上报手机号码 键值
    public static final String REPORT_PHONE_NUMBER = "report_phone_number";
    //车牌号码 键值
    public static final String LICENSE_PLATE_NUMBER = "license_plate_number";
    //上报频度 键值
    public static final String REPORT_TIME_FREQUENCY = "report_time_frequency";

    public static SharedPreferences getInstance(Context context) {
        if (sharedPreferences == null) {
            synchronized (PreferencesUtils.class) {
                if (sharedPreferences == null) {
                    sharedPreferences = context.getSharedPreferences(LOGIN_USER, Context.MODE_PRIVATE);
                }
            }
        }
        return sharedPreferences;
    }

    public static void saveData(Context context, Map<String, Object> list) {
        if (list != null && list.size() > 0) {
            Editor editor = getInstance(context).edit();
            for (String key : list.keySet()) {
                Object value = list.get(key);
                if (value instanceof Integer) {
                    editor.putInt(key, (Integer) value);
                }
                if (value instanceof Long) {
                    editor.putLong(key, (Long) value);
                }
                if (value instanceof Float) {
                    editor.putFloat(key, (Float) value);
                }
                if (value instanceof String) {
                    editor.putString(key, (String) value);
                }
                if (value instanceof Boolean) {
                    editor.putBoolean(key, (Boolean) value);
                }
            }
            editor.commit();
        }
    }

    /**
     * 删除SharedPreferences对应的值
     *
     * @param context
     * @param keys    键值list
     */
    public static void deleteData(Context context, List<String> keys) {
        Editor editor = getInstance(context).edit();
        for (int i = 0; i < keys.size(); i++) {
            if (keys.get(i) != null) {
                editor.remove(keys.get(i));
            }
        }
        editor.commit();
    }

    /**
     * 从SharedPreferences获取对应键值的int值
     *
     * @param context
     * @param key     键值
     * @return
     */
    public static int getInt(Context context, String key) {
        SharedPreferences sp = getInstance(context);
        int value = sp.getInt(key, 0);
        return value;
    }

    /**
     * 从SharedPreferences获取对应键值的String值
     *
     * @param context
     * @param key     键值
     * @return
     */
    public static String getString(Context context, String key) {
        SharedPreferences sp = getInstance(context);
        String value = sp.getString(key, "");
        return value;
    }

    /**
     * 存入对应的键值对
     *
     * @param context
     * @param key     键值
     * @param value   对应的int值
     */
    public synchronized static void setInt(Context context, String key, int value) {
        if (key == null) {
            throw new NullPointerException();
        }
        Editor editor = getInstance(context).edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 存入对应的键值对
     *
     * @param context
     * @param key     键值
     * @param value   对应的String值
     */
    public static void setString(Context context, String key, String value) {
        if (key == null || value == null) {
            throw new NullPointerException();
        }
        Editor editor = getInstance(context).edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 存入对应的键值对
     *
     * @param context
     * @param key     键值
     * @param value   对应的boolean值
     */
    public static void setBoolean(Context context, String key, boolean value) {
        if (key == null) {
            throw new NullPointerException();
        }
        Editor editor = getInstance(context).edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 从SharedPreferences获取对应键值的Boolean值
     *
     * @param context
     * @param key     键值
     * @return
     */
    public static boolean getBoolean(Context context, String key) {
        SharedPreferences sp = getInstance(context);
        boolean value = sp.getBoolean(key, false);
        return value;
    }

    private PreferencesUtils() {
        throw new AssertionError();
    }


    public static Editor getEditor() {
        return getInstance(MainApp.getInstance()).edit();
    }

    /**
     * 删除保存的用户信息
     */
    public static void deleteUserInfo(Context context) {
        //删除对应的登录账号密码
        List<String> list = new ArrayList();
        list.add(Constant.UserInfo.account);
        list.add(Constant.UserInfo.passward);
        list.add(Constant.UserInfo.username);
        list.add(Constant.UserInfo.email);
        list.add(Constant.UserInfo.address);
        list.add(Constant.UserInfo.id);
        list.add(Constant.UserInfo.phone);
        deleteData(context, list);
    }

}
